package com.cheyun.netsalev3.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.Animation
import android.view.animation.Transformation
import com.cheyun.netsalev3.utils.FunctionUtils


/**
 * 百分比圆环
 */
class MyCircleView : View {
    constructor(context: Context) : super(context) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initView()
    }


    private lateinit var paint: Paint
    //圆环画笔
    private lateinit var mProgressRingPaint: Paint
    //圆环宽度
    private var mRingWidth: Float = 0f
    //圆半径
    private var mRadius: Float = 0f

    private lateinit var mTextPaint: Paint

    //圆环大小
    private var circleAgre = 0f
    //临时大小
    private var temCircleAgre = 0f
    //动画器
    private lateinit var annotation: BarAnimation

    private fun initView() {
        mRingWidth = FunctionUtils.dip2px(context, 5f).toFloat()
        paint = Paint()
        paint.isAntiAlias = true// 抗锯齿效果
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = mRingWidth
        paint.color = Color.parseColor("#FFEDF0F5")

        mProgressRingPaint = Paint()
        mProgressRingPaint.isAntiAlias = true// 抗锯齿效果
        mProgressRingPaint.style = Paint.Style.STROKE
        mProgressRingPaint.strokeCap = Paint.Cap.ROUND// 圆形笔头
        mProgressRingPaint.strokeWidth = mRingWidth
        mProgressRingPaint.color = Color.parseColor("#FF419BFC")


        mTextPaint = Paint()
        mTextPaint.isAntiAlias = true// 抗锯齿效果
        mTextPaint.style = Paint.Style.FILL
        mTextPaint.textAlign = Paint.Align.CENTER
        mTextPaint.color = Color.parseColor("#ff4a566e")// 字体颜色
        mTextPaint.textSize = FunctionUtils.sp2px(context, 19f).toFloat()

        annotation = BarAnimation()
        annotation.duration = 300
        annotation.interpolator = AccelerateDecelerateInterpolator()


    }

    public fun setText(color: String, float: Float) {
        //圆环颜色
        mProgressRingPaint.color = Color.parseColor(color)
        //百分比
        circleAgre = float
        startAnimation(annotation)
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        // 圆心坐标,当前View的中心
        var x = width / 2f
        var y = height / 2f

        //如果未设置半径，则半径的值为view的宽、高一半的较小值
        mRadius = Math.min(width / 2, height / 2).toFloat()
        mRadius -= mRingWidth / 2
        // 底环
        canvas?.drawCircle(x, y, mRadius, paint)

        var left = x - mRadius
        var top = y - mRadius
        var right = x + mRadius
        var bottom = y + mRadius
        //圆环
        canvas?.drawArc(RectF(left, top, right, bottom),
                -90f, temCircleAgre, false, mProgressRingPaint)
        var tem =  (circleAgre * 100) .toInt()
        var text = tem.toString() + "%"
        //字体
        canvas?.drawText(text, x, getFontHeight(), mTextPaint)
    }

    private fun getFontHeight(): Float {
        var fontMetrics = mTextPaint.fontMetrics
        var top = fontMetrics.top
        var bottom = fontMetrics.bottom
        return (height / 2 - top / 2 - bottom / 2)
    }


    /**
     * 进度条动画
     */
    inner class BarAnimation : Animation() {

        /**
         * 然后调用postInvalidate()不停的绘制view。
         */
        override fun applyTransformation(interpolatedTime: Float, t: Transformation) {

            super.applyTransformation(interpolatedTime, t)
            temCircleAgre = interpolatedTime * circleAgre * 360
            postInvalidate()
        }
    }


}